package org.rufolfheszele.andpixelbyandpixel.configuration;

import java.util.TreeMap;

import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.graphics.Point;

/**
 * This class is holding configuration data related to one vehicle on a level
 *
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 * */
public class Vehicle
{
		/**
		 * The only constructor takes the XML node and reads every data into its data members
		 *
		 * @param node The XML node containing the network configuration
		 */
		public Vehicle(Node node)
		{
			NodeList children = node.getChildNodes();
			NamedNodeMap attributes = node.getAttributes();

			mNode = node;
			for(int i = 0; i < attributes.getLength(); ++i)
			{
				Attr attribute = (Attr)attributes.item(i);
				
				if(true == attribute.getName().equals("name"))
				{
					mName = attribute.getValue();
					break;
				}
			}
			if(null == mName)
				mName = "name";
			mPosition = new Point();
			for(int i = 0; i < children.getLength(); ++i)
			{
				Node child = children.item(i);
				
				if(true == child.getNodeName().equals("position"))
				{
					NamedNodeMap childAttributes = child.getAttributes();
					
					for(int j = 0; j < childAttributes.getLength(); ++j)
					{
						Attr attribute = (Attr)childAttributes.item(j);
						
						if(true == attribute.getName().equals("name"))
						{
							if(true == attribute.getValue().equals("x"))
								mPosition.x = Integer.parseInt(child.getChildNodes().item(0).getNodeValue());
							else if(true == attribute.getValue().equals("y"))
								mPosition.y = Integer.parseInt(child.getChildNodes().item(0).getNodeValue());
						}
					}
				}
			}
			mCachedPosition = mPosition;
		}
		
		public String getName()
		{
			return mName;
		}
		
		public void setName(String name)
		{
			NamedNodeMap attributes = mNode.getAttributes();

			mName = name;
			for(int i = 0; i < attributes.getLength(); ++i)
			{
				Attr attribute = (Attr)attributes.item(i);
				
				if(true == attribute.getName().equals("name"))
				{
					attribute.setValue(name);
					break;
				}
			}
		}
		
		public Point getPosition()
		{
			return mCachedPosition;
		}
		
		public void setPosition(Point position)
		{
			String nodeContent;
			TreeMap<String, String> properties = new TreeMap<String, String>();

			mPosition = position;
			mCachedPosition = position;
			nodeContent = Integer.toString(position.x);
			properties.put("name", "x");
			Configuration.updateNodeContent(mNode, "position", nodeContent, properties);
			nodeContent = Integer.toString(position.y);
			properties.put("name", "y");
			Configuration.updateNodeContent(mNode, "position", nodeContent, properties);
		}
		
		/**
		 * Caches the given position, but will not be written to the xml file
		 *
		 * @param position The position to be cached
		 */
		public void cachePosition(Point position)
		{
			mCachedPosition = position;
		}
		
		/**
		 * Clears the cashed position
		 */
		
		public void resetCache()
		{
			mCachedPosition = mPosition;
		}

		private String mName			= null;
		private Point mPosition			= null;
		private Point mCachedPosition	= null;
		private Node mNode				= null;
};